<?php

class Admin_pointController extends Admin_baseController {
	public function listAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$title = Request::getParams('title');
		$where[] = 'status=1';
		if ($title) {
			$where[] = 'title like ?';
			$value[] = '%' . $title . '%';
			$url['title'] = $title;
		}
		$total = self::model('point_goods')->count('id', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'title' => $title,
			'total' => $total,
			'list' => self::model('point_goods')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_point/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	public function ruleAction() {
		$id = 1;
		if (Request::isGet()) {
			$this->assign('info', self::model('point_rule')->getOne('id=?', $id));
			$this->display();
			exit;
		}
		$data = [];
		$data['dynamic_type'] = intval(Request::getParams('dynamic_type'));
		$data['dynamic_per'] = floatval(Request::getParams('dynamic_per'));
		$data['dynamic_share_type'] = intval(Request::getParams('dynamic_share_type'));
		$data['dynamic_share_per'] = floatval(Request::getParams('dynamic_share_per'));
		$data['share_type'] = intval(Request::getParams('share_type'));
		$data['share_per'] = floatval(Request::getParams('share_per'));
		$data['dealer_price'] = floatval(Request::getParams('dealer_price'));
		$data['shop_price'] = floatval(Request::getParams('shop_price'));
		$data['reg_rebate'] = floatval(Request::getParams('reg_rebate'));
		$data['shop_rebate'] = floatval(Request::getParams('shop_rebate'));
		$data['derler_rebate'] = floatval(Request::getParams('derler_rebate'));
		$data['regs_rebate'] = floatval(Request::getParams('regs_rebate'));
		$data['shops_rebate'] = floatval(Request::getParams('shops_rebate'));
		$data['derlers_rebate'] = floatval(Request::getParams('derlers_rebate'));
		$data['flagship_image'] = Request::getParams('flagship_image');
		$data['shop_image'] = Request::getParams('shop_image');
		$data['daili_rebate'] = floatval(Request::getParams('daili_rebate'));
		$data['is_show'] = intval(Request::getParams('is_show'));

		if ($id) {
//修改
			if (self::model('point_rule')->update($data, 'id=?', $id)) {
				Response::ajax(200, '修改成功');
			}
		} else {
//新增
			if (self::model('point_rule')->insert($data)) {
				Response::ajax(200, '添加成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function editAction() {
		$id = Request::getParams('id');
		$data = [];
		$data['title'] = Request::getParams('title');
		$data['image'] = Request::getParams('image');
		$data['point'] = intval(Request::getParams('point'));
		$data['description'] = Html::decode(Request::getParams('description'));
		$data['total'] = intval(Request::getParams('total'));
		$data['sort'] = intval(Request::getParams('sort'));
		$data['is_recommend'] = intval(Request::getParams('is_recommend'));
		$data['status'] = 1;
		$pointGoodsInfo = [];
		if ($id && !$pointGoodsInfo = self::model('point_goods')->getOne('id=?', $id)) {
			Response::showMsg('信息查询失败，请稍后重试');
		}
		if (Request::isGet()) {
			$this->assign('pointGoodsInfo', $pointGoodsInfo);
			$this->display();
			exit;
		}
		if ($id) {
//修改
			if (self::model('point_goods')->update($data, 'id=?', $id)) {
				Response::ajax(200, '修改成功');
			}
		} else {
//新增
			$data['add_time'] = $data['update_time'] = date('Y-m-d H:i:s');
			if (self::model('point_goods')->insert($data)) {
				Response::ajax(200, '添加成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
	}

	public function deleteAction() {
		$id = Request::getParams('id');
		if (!$id || !$info = self::model('point_goods')->getOne('id=?', $id)) {
			Response::ajax(NORMAL_ERROR, '信息查询失败,删除失败');
		}
		if (self::model('point_goods')->update(['status' => 2], 'id=?', $id)) {
			Response::ajax(200, '删除成功');
		}
		Response::ajax(NORMAL_ERROR, '删除失败,请稍后再试');
	}

	public function orderListAction() {
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 20);
		$where = $value = $url = array();
		$title = Request::getParams('title');
		$orderNo = Request::getParams('orderNo');
		$payStatus = strval(Request::getParams('payStatus', -1));
		$orderStatus = strval(Request::getParams('orderStatus', -1));
		if ($orderNo) {
			$where[] = 'order_no like ?';
			$value[] = '%' . $orderNo . '%';
			$url['orderNo'] = $orderNo;
		}
		if ($title) {
			$where[] = 'title like ?';
			$value[] = '%' . $title . '%';
			$url['title'] = $title;
		}
		if ($orderStatus != -1) {
			$where[] = 'order_status=?';
			$value[] = $orderStatus;
			$url['orderStatus'] = $orderStatus;
		}
		if ($payStatus != -1) {
			$where[] = 'pay_status=?';
			$value[] = $payStatus;
			$url['payStatus'] = $payStatus;
		}
		$total = self::model('point_order')->count('id', $where, $value);
		$this->assign(array(
			'page' => $page,
			'pageSize' => $pageSize,
			'orderNo' => $orderNo,
			'title' => $title,
			'payStatus' => $payStatus,
			'payStatusList' => self::model('point_order')->getPayStatusList(),
			'orderStatus' => $orderStatus,
			'orderStatusList' => self::model('point_order')->getOrderStatusList(),
			'total' => $total,
			'list' => self::model('point_order')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value),
			'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_point/orderList?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
		));
		$this->display();
	}

	/**
	 * 订单发货
	 */
	public function orderPostAction() {
		$orderId = intval(Request::getParams('orderId'));
		if (!$orderId || !$orderInfo = self::model('point_order')->getOne('id=?', $orderId)) {
			Response::showMsg('订单信息获取失败,请稍后');
		}
		if ($orderInfo['pay_status'] != orderModel::ORDER_STATUS_PAIED) {
			Response::showMsg('订单未支付成功,不能发货');
		}
		$data = [];
		$data['express_no'] = Request::getParams('express_no');
		$data['express_com'] = Request::getParams('express_com');
		if (Request::isGet()) {
			$this->assign([
				'expressCom' => self::model('order')->getExpressCom(),
				'orderInfo' => $orderInfo,
			]);
			$this->display();
			exit;
		}
		$data['post_time'] = time();
		$data['order_status'] = orderModel::ORDER_STATUS_POST;
		$data['update_time'] = time();
		if (self::model('point_order')->update($data, 'id=?', $orderId)) {
			Response::ajax(200, '订单发货成功');
		}
		Response::ajax(NORMAL_ERROR, '订单发货失败');
	}

	/**
	 * 订单详情
	 */
	public function orderInfoAction() {
		$orderId = intval(Request::getParams('orderId'));
		if (!$orderId || !$orderInfo = self::model('point_order')->getOne('id=?', $orderId)) {
			Response::showMsg('订单信息获取失败,请稍后');
		}
		$this->assign([
			'payStatusList' => self::model('point_order')->getPayStatusList(),
			'orderStatusList' => self::model('point_order')->getOrderStatusList(),
			'expressCom' => self::model('order')->getExpressCom(),
			'orderInfo' => $orderInfo,
		]);
		$this->display();
	}
}